package com.relife.common.model;

import java.util.Map;


import com.relife.common.exception.BusinessException;
import com.relife.common.utils.StringUtil;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.apache.log4j.Logger;

/**
 * $.ajax后需要接受的JSON
 * 
 * @author
 * 
 */
public class AjaxJson {
	public static Logger log = Logger.getLogger(AjaxJson.class);
	private boolean success = true;// 是否成功
	private String msg = null;// 提示信息
	private Object obj = null;// 其他信息
	private Map<String, Object> attributes;// 其他参数

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public String getMsg() {
		if (StringUtil.isEmpty(this.msg)) {
			return this.success ? "操作成功。" : "操作失败！";
		}
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setException(Exception e) {
		this.setSuccess(false);
		log.error(StringUtil.exception2String(e));
		if ("java.lang.RuntimeException".equals(e.getClass().getName())) {
			this.setMsg(e.getMessage());
		} else if (e instanceof BusinessException) {
			this.setMsg(e.getMessage());
		} else if (e instanceof ActivitiObjectNotFoundException) {
			this.setMsg("任务已经办理，请不要重复操作！");
		}else {
			this.setMsg("操作失败！");
		}
	}
}
